#!/bin/bash

# 检查服务是否启动
check_service() {
    local service_name=$1
    local pid

    # 查找进程ID
    pid=$(pgrep -f "$service_name")

    if [ -z "$pid" ]; then
        echo "服务 $service_name 未启动。"
        return 1
    else
        echo "服务 $service_name 已启动，进程ID: $pid"
        return 0
    fi
}

# 终止服务
stop_service() {
    local service_name=$1
    local pids

    # 查找进程ID
    pids=$(pgrep -f "$service_name")

    if [ -z "$pids" ]; then
        echo "服务 $service_name 未启动，无需终止。"
    else
        echo "正在终止服务 $service_name (进程ID: $pids)..."
        for pid in $pids; do
            kill -9 "$pid"
            if [ $? -eq 0 ]; then
                echo "进程 $pid 已成功终止。"
            else
                echo "终止进程 $pid 失败。"
            fi
        done
    fi
}


# 主逻辑函数
manage_service() {
    local service_name=$1

    # 检查服务是否启动
    if check_service "$service_name"; then
        # 提示用户是否终止进程
        read -p "是否终止服务 $service_name？(y/n): " user_input
        if [ "$user_input" = "y" ] || [ "$user_input" = "Y" ]; then
            stop_service "$service_name"
        else
            echo "用户取消终止操作。"
        fi
    fi
}



# 示例调用
service_name="TIMS"
manage_service "$service_name"


